const state = {
  typePage: {},
  typeList: [],
  list: [],
  page: {}
}

const actions = {
  saveTypes: ({ commit }, payload) => {
    commit('SAVETYPES', payload)
  },
  saveList: ({ commit}, payload) => {
    commit('SAVELIST', payload)
  }
}

const mutations = {
  SAVETYPES: (state, payload) => {
    state.typeList = payload.result,
    state.typePage = {
      size: 10,
      number: payload.number || 1,
      total: payload.resultNum
    }
  },
  SAVELIST: (state, payload) => {
    if (payload.result.length === 0) {
      state.list = []
    } else {
      state.list = payload.result.map(item => {
          return {
              id: item.id,
              publicAt: item.public_at,
              title: item.title,
              subTitle: item.sub_title,
              isTop: item.is_top,
              content: item.content,
              typeId: item.type_id,
              origin: item.origin,
              author: item.author,
              state: item.state,
              imgUrl: item.img_url,
              attachName: item.attach_name,
              attachUrl: item.attach_url,
              clickTimes: item.click_times,
              description: item.description
          }
      })
    }
    state.page = {
      size: 10,
      number: payload.number || 1,
      total: payload.resultNum
    }
  }
}

export default {
  state,
  actions,
  mutations
}